package com.smartens.boit.activity;


import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.smartens.boit.R;
import com.smartens.boit.base.BaseActivity;
import com.smartens.boit.bean.EnergyConsumptionBean;
import com.smartens.boit.bean.StatusDetailBean;
import com.smartens.boit.util.HttpHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 设备管理能耗
 * @Author songtt
 * @Date 2021/3/4
 */


public class EquipmentEnergyActivity extends BaseActivity {
    private static final String TAG = "EquipmentEnergyActivity";
    private HttpHelper helper;
    private BarChart barChart;
    private YAxis leftAxis;             //左侧Y轴
    private YAxis rightAxis;            //右侧Y轴
    private XAxis xAxis;                //X轴
    private Legend legend;              //图例

    private TextView DevicesNum;
    private TextView DevicesName;
    private TextView TotalPower;
    private TextView CurrentVoltage1;
    private TextView CurrentElectric1;


    private LinearLayout back;
    private List<Integer> listData = new ArrayList<>();
    private List<Integer> listData2 = new ArrayList<>();
    private List<String> xValues = new ArrayList<>();
    private List<String> yValues = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_equipment_energy);
        //返回
        back = findViewById(R.id.tv_back);
        CurrentElectric1 = findViewById(R.id.CurrentElectric1);
        CurrentVoltage1 = findViewById(R.id.CurrentVoltage1);
        TotalPower = findViewById(R.id.TotalPower);
        DevicesNum = findViewById(R.id.DevicesNum);
        DevicesName = findViewById(R.id.DevicesName);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EquipmentEnergyActivity.this.finish();
            }
        });
        //图表
        barChart = findViewById(R.id.chart);

        for (int i = 1; i < 10; i++) {
            listData.add(i + 10);
            listData2.add(i + 15);

        }
        //网络请求
        helper = HttpHelper.getInstance();
        getDeviceEneygyList();
        GetDeviceStatusListResult();

    }


    /**
     * 初始化BarChart图表
     */
    private void initBarChart(BarChart barChart) {
        /***图表设置***/
        //背景颜色
        barChart.setBackgroundColor(Color.WHITE);
        //不显示图表网格
        barChart.setDrawGridBackground(false);
        //背景阴影
        barChart.setDrawBarShadow(false);
        barChart.setHighlightFullBarEnabled(false);
        //显示边框
        barChart.setDrawBorders(false);

        //不显示右下角描述内容
        Description description = new Description();
        description.setEnabled(false);
        barChart.setDescription(description);
        /***XY轴的设置***/
        //X轴设置显示位置在底部
        xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setAxisMinimum(0f);
        xAxis.setGranularity(1f);
        //X轴横坐标显示的数量
       // xAxis.setLabelCount(7);
        xAxis.setLabelCount(18);
        // 不显示X轴 Y轴线条
        xAxis.setDrawAxisLine(false);
        xAxis.setDrawGridLines(false);
        leftAxis = barChart.getAxisLeft();
        rightAxis = barChart.getAxisRight();
        //设置Y轴 与值的空间空隙 这里设置30f意为30%空隙，默认是10%
//        leftAxis.setSpaceTop(10f);

        //保证Y轴从0开始，不然会上移一点
        leftAxis.setAxisMinimum(0f);
        rightAxis.setAxisMinimum(0f);

        //右侧Y轴网格线设置为虚线
        YAxis yAxis = barChart.getAxisLeft();
        yAxis.setLabelCount(6);
        yAxis.enableGridDashedLine(10f, 10f, 0f);
        // 设置x轴轴线的宽度
        xAxis.setAxisLineWidth(1f);
        // 设置轴线的颜色
        xAxis.setAxisLineColor(Color.parseColor("#A4A4A4"));
        // 设置坐标颜色
        xAxis.setTextColor(Color.parseColor("#A4A4A4"));
        // 去掉右侧 的坐标轴
        barChart.getAxisRight().setEnabled(false);
        // 设置轴线的颜色
        yAxis.setAxisLineColor(Color.parseColor("#A4A4A4"));
        // 设置坐标颜色
        yAxis.setTextColor(Color.parseColor("#A4A4A4"));
//        柱子宽度  ？？？？？
        barChart.setVisibleXRangeMaximum(8);
        barChart.setDoubleTapToZoomEnabled(false);
        //禁止拖拽
        barChart.setDragEnabled(false);
        //X轴或Y轴禁止缩放
        barChart.setScaleXEnabled(false);
        barChart.setScaleYEnabled(false);
        barChart.setScaleEnabled(false);
        //禁止所有事件
        barChart.setTouchEnabled(false);
        /***折线图例 标签 设置***/
        legend = barChart.getLegend();
        barChart.getLegend().setEnabled(false); // 不显示图例

        //是否绘制在图表里面
        legend.setDrawInside(false);
        showBarChart(yValues, "", getResources().getColor(R.color.white));
    }


    /**
     * 柱状图始化设置 一个BarDataSet 代表一列柱状图
     *
     * @param barDataSet 柱状图
     * @param color      柱状图颜色
     */
    private void initBarDataSet(BarDataSet barDataSet, int color) {
        barDataSet.setColor(color);
        //不显示柱状图顶部值
        barDataSet.setDrawValues(false);
        //设置柱形的颜色
        barDataSet.setColor(getResources().getColor(R.color.myblue));

    }

    public void showBarChart(List<String> dateValueList, String name, int color) {
        ArrayList<BarEntry> entries = new ArrayList<>();
        for (int i = 0; i < dateValueList.size(); i++) {
            /**
             * 此处还可传入Drawable对象 BarEntry(float x, float y, Drawable icon)
             * 即可设置柱状图顶部的 icon展示
             */
            BarEntry barEntry = new BarEntry(i, Float.parseFloat(dateValueList.get(i)));
            entries.add(barEntry);
        }
        //每一个BarDataSet代表一类柱状图
        BarDataSet barDataSet = new BarDataSet(entries, name);
        initBarDataSet(barDataSet, color);
//
        //X轴自定义值
        xAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
//                return xValues.get((int)value);
                int index = (int) value;/*
                if (index >= 0 && index < 6) {
                    return xValues.get(index);
                } else {
                    return "";
                }*/
                if (!(index % 5 == 0)) {
                    return "";
                } else {
                    return xValues.get(index);
                }
            }
        });

        BarData data = new BarData(barDataSet);
        barChart.setData(data);
    }


    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    public void getDeviceEneygyList() {
        helper.GetDeviceEneygyList(new HttpHelper.HttpCallBack<EnergyConsumptionBean>() {

            @SuppressLint("LongLogTag")
            @Override
            public void onSuccess(List<EnergyConsumptionBean> result) {
                for (int i = 0; i < result.size(); i++) {
                    result.get(i).getTotalTime();
                    result.get(i).getTotalEnergy();
                    xValues.add(result.get(i).getTotalTime().substring(5));

                    yValues.add(result.get(i).getTotalEnergy());
                }
                // Log.v("====GetDeviceEneygyList==获取成功", String.valueOf(result.size()));
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        initBarChart(barChart);
                    }
                });
            }

            @SuppressLint("LongLogTag")
            @Override
            public void onFailure(final String message) {
                //   Log.v("=====GetDeviceEneygyList==onFailure", message);
                barChart.setNoDataText("暂无数据");
            }
        });
    }

    public void GetDeviceStatusListResult() {
        helper.GetDeviceStatusListResult(new HttpHelper.HttpCallBack<StatusDetailBean>() {

            @SuppressLint("LongLogTag")

            @Override
            public void onSuccess(List<StatusDetailBean> result) {
                if (result.size() > 0) {
                    Log.d(TAG, "onSuccess: GetDeviceStatusListResult" + result.get(0));
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            DevicesName.setText(result.get(0).getDevicesName());
                            DevicesNum.setText("设备编号 " + result.get(0).getDevicesNum());
                            TotalPower.setText(result.get(0).getTotalPower());
                            CurrentVoltage1.setText(result.get(0).getCurrentVoltage1());
                            CurrentElectric1.setText(result.get(0).getCurrentElectric1());

                        }
                    });
                } else {
                    Log.d(TAG, "onSuccess: GetDeviceStatusListResult" + 0);

                }


            }

            @SuppressLint("LongLogTag")
            @Override
            public void onFailure(final String message) {
                //   Log.v(TAG, "GetDeviceStatusList" + message);
            }
        });
    }
}